package com.wenli.togglebuttondemo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

import com.wenli.togglebuttondemo.view.ToggleButton;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ToggleButton mToggleButton = (ToggleButton) findViewById(R.id.togglebutton);

        // 给开关控件设置背景图片.
        mToggleButton.setSwitchBackgroundResource(R.drawable.switch_background);
        // 设置开关滑动块的图片.
        mToggleButton.setSlideButtonBackgroundResource(R.drawable.slide_button_background);

        // 设置当前开关默认的状态. true 打开, false 关闭
        mToggleButton.setCurrentState(true);

         //给开关设置监听
        mToggleButton.setOnToggleStateChangedListener(new ToggleButton.OnToggleStateChangedListener() {

            @Override
            public void onToggleStateChanged(boolean currentState) {
                if (currentState) {
                    Toast.makeText(MainActivity.this, "开关打开", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "开关关闭", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

}
